import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/entity/widget_entity.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/modle/person/bloc/bank_list_bloc.dart';
import 'package:shopping_group/modle/person/entity/cust_bankInfo_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/utils.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:shopping_group/view/button_progress_view.dart';
import 'package:bot_toast/bot_toast.dart';

class BankDetailsPage extends StatefulWidget {
  final CustBankInfoEntity entity;
  final String txType;
  BankDetailsPage({Key key, this.entity, this.txType}) : super(key: key);

  @override
  _BankDetailsPageState createState() => _BankDetailsPageState();
}

class _BankDetailsPageState extends State<BankDetailsPage> {
  WidgetEntity _jeWidgetEntity = WidgetEntity();
  BankListBloc _bloc;
  // String _dz;
  // String _fw;
  String _maxJe = '0';
  StreamSubscription _refresSubscription;
  @override
  void initState() {
    super.initState();
    _bloc = BankListBloc();
    _jeWidgetEntity = WidgetUtils.initEditText('提现￥', '', '请输入金额', 'number', TextEditingController());
    // Future.delayed(Duration(milliseconds: 1000), () {
    //   _showAdView();
    // });
    if (this.widget.txType.contains('推广费')) {
      Map<String, dynamic> data = {};
      data['result'] = BaseConstant.userId;
      // 推广费：GMT_TGF、福利金：GMT_FULI、津贴：GMT_JINTIE"
      data['remark'] = 'GMT_TGF';
      _bloc.kytxDate.add(data);
    }
    _refresSubscription = eventBus.on<MaxMoneyEvent>().listen((event) {
      setState(() {
        _maxJe = event.je;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, this.widget.txType),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SafeArea(
          child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            reverse: false,
            padding: EdgeInsets.all(0.0),
            physics: BouncingScrollPhysics(),
            child: Container(
              decoration: BoxDecoration(
                color: Colours.gray_f0,
              ),
              child: Column(children: <Widget>[
                Container(
                  padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 8),
                  color: Colours.title_white,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          SizedBox(
                            height: 4,
                          ),
                          WidgetUtils.buildNetworkImageView(this.widget.entity.imageUrl),
                          SizedBox(
                            height: 4,
                          ),
                          WidgetUtils.getTextColorView(
                            this.widget.entity.bankTypeName,
                            Colours.text_2222,
                            Dimens.FONT_SIZE_16,
                          ),
                          SizedBox(
                            height: 4,
                          ),
                        ],
                      ),
                      Expanded(
                        child: Container(
                          color: Colours.transparent,
                          alignment: Alignment.center,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              WidgetUtils.getTextColorView(
                                this.widget.entity.ownerName,
                                Colours.text_2222,
                                Dimens.FONT_SIZE_16,
                              ),
                              SizedBox(
                                height: 8,
                              ),
                              WidgetUtils.getTextColorView(
                                this.widget.entity.bankNo,
                                Colours.text_2222,
                                Dimens.FONT_SIZE_16,
                              ),
                              SizedBox(
                                height: 8,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 4,
                ),
                WidgetUtils.getTextColorView(
                  '当前电商总账余额${BaseConstant.dsAllAcct}',
                  // this.widget.txType.contains('推广费')
                  //     ? '当前推广费余额:${BaseConstant.integralAccount}'
                  //     : (this.widget.txType.contains('福利金')
                  //         ? '当前福利金余额:${BaseConstant.fuliAcct}'
                  //         : '当前津贴余额:${BaseConstant.jinTieAcct}'),
                  Colours.text_2222,
                  Dimens.FONT_SIZE_16,
                ),
                this.widget.txType.contains('推广费')
                    ? WidgetUtils.getTextColorView(
                        '可提取最高额:$_maxJe',
                        Colours.text_2222,
                        Dimens.FONT_SIZE_16,
                      )
                    : Container(),
                SizedBox(
                  height: 4,
                ),
                WidgetUtils.showEditText(_jeWidgetEntity, onChangedText: (data) {
                  // setState(() {
                  //   if (ObjectUtil.isEmptyString(data)) {
                  //     _dz = '';
                  //     _fw = '';
                  //   }
                  //   if (null != data && data is String && data.length >= 1) {
                  //     _dz = Utils.formatNum(
                  //         (double.parse(_jeWidgetEntity.controller.text) *
                  //             0.94),
                  //         2);
                  //     _fw = Utils.formatNum(
                  //         (double.parse(_jeWidgetEntity.controller.text) *
                  //             0.06),
                  //         2);
                  //   }
                  // });
                }),
                SizedBox(
                  height: 8,
                ),
                // Row(
                //   mainAxisAlignment: MainAxisAlignment.center,
                //   children: <Widget>[
                //     WidgetUtils.getTextColorView(
                //       '可到账 ：',
                //       Colours.text_2222,
                //       Dimens.FONT_SIZE_16,
                //     ),
                //     WidgetUtils.getTextColorView(
                //       ObjectUtil.isEmptyString(_dz) ? '' : _dz,
                //       Colours.text_2222,
                //       Dimens.FONT_SIZE_16,
                //     ),
                //     SizedBox(
                //       width: 4,
                //     ),
                //     WidgetUtils.getTextColorView(
                //       '服务费 ：',
                //       Colours.text_2222,
                //       Dimens.FONT_SIZE_16,
                //     ),
                //     WidgetUtils.getTextColorView(
                //       ObjectUtil.isEmptyString(_fw) ? '' : _fw,
                //       Colours.text_2222,
                //       Dimens.FONT_SIZE_16,
                //     ),
                //   ],
                // ),
                SizedBox(
                  height: 8,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      SizedBox(
                        height: 4,
                      ),
                      WidgetUtils.getTextColorView('每日只能提现一次提现规则', Colours.text_2222, Dimens.FONT_SIZE_18,
                          fontweight: FontWeight.w500),
                      SizedBox(
                        height: 4,
                      ),
                      WidgetUtils.getTextColorView(
                        '1、提现时间：每周五提现，当日只能提现一次，节假日不能提现；',
                        Colours.text_2222,
                        Dimens.FONT_SIZE_16,
                      ),
                      SizedBox(
                        height: 4,
                      ),
                      WidgetUtils.getTextColorView(
                        '2、开放提现时间09：00—17：00；',
                        Colours.text_2222,
                        Dimens.FONT_SIZE_16,
                      ),
                      SizedBox(
                        height: 4,
                      ),
                      WidgetUtils.getTextColorView(
                        '3、提现服务费5%；',
                        Colours.text_2222,
                        Dimens.FONT_SIZE_16,
                      ),
                      SizedBox(
                        height: 4,
                      ),
                      WidgetUtils.getTextColorView(
                        '4、到账时间为提现日下周的周一、周二24:00前',
                        Colours.text_2222,
                        Dimens.FONT_SIZE_16,
                      ),
                      SizedBox(
                        height: 4,
                      ),
                      WidgetUtils.getTextColorView(
                        '5、每次提现最低50元',
                        Colours.text_2222,
                        Dimens.FONT_SIZE_16,
                      ),
                      SizedBox(
                        height: 4,
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 32,
                ),
                ButtonProgressView.crateCupertinoView('确认提现', blackColor: Colours.themeColor, cupertinoCall: () {
                  String data = _jeWidgetEntity.controller.text;
                  if (ObjectUtil.isEmptyString(data)) {
                    BotToast.showText(text: '请输入提现余额');
                    return;
                  }
                  if (null == data || !(data is String) || '0' == data) {
                    return;
                  }
                  // int temp = int.parse(data) % 100;
                  // if (data.length < 3 || temp != 0) {
                  //   BotToast.showText(text: '提现必须是100的倍数');
                  //   return;
                  // }
                  _showTxView(int.parse(data));
                }),
              ]),
            ),
          ),
        ),
      ),
    );
  }

  Future _showTxView(int je) async {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (context) {
          return AlertDialog(
            title: Center(
              child: WidgetUtils.getTextColorView(
                '选择',
                Colours.text_2222,
                Dimens.FONT_SIZE_16,
              ),
            ),
            content: WidgetUtils.getTextColorView('确定要提现$je元？', Colours.text_2222, Dimens.FONT_SIZE_16),
            actions: <Widget>[
              FlatButton(
                  onPressed: () {
                    Navigator.of(context).pop('yes');
                  },
                  child: WidgetUtils.getTextColorView('确定', Colours.text_2222, Dimens.FONT_SIZE_16)),
              FlatButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: WidgetUtils.getTextColorView('取消', Colours.text_2222, Dimens.FONT_SIZE_16)),
            ],
          );
        }).then((var result) {
      if (null == result) {
        return;
      }

      if (result is String && 'yes' == result) {
        Map<String, dynamic> data = {};
        data['val'] = je;
        data['userId'] = this.widget.entity.userId;
        data['bankInfoId'] = this.widget.entity.id;
        _bloc.txDate.add(data);
        // this.widget.txType.contains('推广费')
        //     ? _bloc.txDate.add(data)
        //     : this.widget.txType.contains('福利金')
        //         ? _bloc.fljtxDate.add(data)
        //         : _bloc.jttxDate.add(data);
      }
    });
  }

  Future _showAdView() async {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (context) {
          return showAlertDialog();
        }).then((var result) {
      if (null == result) {
        return;
      }

      if (result is String && 'yes' == result) {}
    });
  }

  Widget showAlertDialog() {
    return SimpleDialog(
      title: WidgetUtils.getTextColorView('提示', Colours.text_2222, Dimens.FONT_SIZE_18),
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 4),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 24, horizontal: 8),
                child: WidgetUtils.getTextColorView(
                    '内测期间，为了保证账户资金安全，仅提供提现体验服务，单笔提现额度不能超过10000。', Colours.gray_91, Dimens.FONT_SIZE_16),
              ),
              WidgetUtils.drawCodeDivider(height: 1, color: Colours.blue_be9),
              SizedBox(
                height: 16,
              ),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pop('yes');
                },
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 8),
                  child: WidgetUtils.getTextColorView('确定', Colours.blue_be9, Dimens.FONT_SIZE_20),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  @override
  void dispose() {
    _jeWidgetEntity.controller?.dispose();
    _refresSubscription?.cancel();
    super.dispose();
  }
}
